package com.example.sharpshopdemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sharpshopdemo.entity.Power;
import com.example.sharpshopdemo.entity.Result;
import com.example.sharpshopdemo.entity.Role;
import com.example.sharpshopdemo.mapper.PowerMapper;
import com.example.sharpshopdemo.mapper.RoleMapper;
import com.example.sharpshopdemo.service.PowerService;
import com.example.sharpshopdemo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.role;

@Service
public class PowerServiceImpl extends ServiceImpl<PowerMapper, Power> implements PowerService {

    @Autowired
    private Result result;

    @Autowired
    private PowerMapper powerMapper;

    @Override
    public Result getAll() {
        result.setSuccess(true);
        result.setData(this.list());
        return result;
    }


    // 插入数据
    public Result insertData(Power power) {
        Result result = new Result(); // 假设Result是一个用于封装响应结果的类
        boolean success = powerMapper.insert(power) > 0;
        result.setSuccess(success);
        return result;
    }

    // 更新数据
    public Result updateData(Power power) {
        Result result = new Result();
        boolean success = powerMapper.updateById(power) > 0; // 假设updateById是根据DataDict中的某个唯一标识更新数据
        result.setSuccess(success);
        return result;
    }

    // 删除数据
    public Result deleteData(Long id) { // 假设我们根据ID来删除数据
        Result result = new Result();
        boolean success = powerMapper.deleteById(id) > 0; // 假设deleteById接受一个Long类型的ID作为参数
        result.setSuccess(success);
        return result;
    }

    // 查询数据（这里只是一个示例，具体实现取决于查询需求）
    public Result<List<Power>> findPowersByNameLike(String keyword) {
        List<Power> powers = powerMapper.selectPowersByNameLike(keyword);
        Result<List<Power>> result = new Result<>();
        if (powers != null && !powers.isEmpty()) {
            result.setSuccess(true);
            result.setData(powers);
        } else {
            result.setSuccess(false);
            result.setErrorMsg("未找到匹配的角色");
        }
        return result;
    }
}
